package com.js.controller;

import com.js.mapper.TeacherMapper;

import com.js.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("teacher")
public class TeacherLogin {
    @Autowired
    private TeacherMapper teacherMapper;
    //跳转登录界面
    @RequestMapping("/jumpLogin")
    public String jumpLogin(HttpSession session){
        if (session.getAttribute("teacherName")!=null){
            return "teacher/backstage";
        }
        return "teacher/teacherLogin";
    }
    @RequestMapping("/jumpFalse")
    public String jumpFalse(){
        return "teacher/codeFalse";
    }
    //登录时提交form表单验证的请求
    @RequestMapping("/login")
    public String login(Teacher teacher, HttpSession session){
        Teacher queryByAccount = teacherMapper.queryTeacherByAccount(teacher.getAccount());
        if (queryByAccount==null){
            return "redirect:/teacher/jumpFalse";
        }
        if (queryByAccount.getPassword().equals(teacher.getPassword())){
            session.setAttribute("teacherName",queryByAccount.getName());
            session.setAttribute("QTeacher",queryByAccount);
            return "teacher/backstage";
        }else {
            return "redirect:/teacher/jumpFalse";
        }
    }
    //退出登录
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.invalidate();
        return  "../index";
    }
}
